package dispatcher.gui.event;

import state.group.events.EventCollection;

public class PersonStatusUpdateThreadPool{
	public static PersonStatusUpdateThreadPool instance = new PersonStatusUpdateThreadPool();
	public static PersonStatusUpdateThreadPool getInstance(){
		return instance;
	}
	
	public static final int MAX_THREADS = 8;
	
	private PersonStatusUpdateThread []threadPool;
	private int curThreads;
	
	public PersonStatusUpdateThreadPool(){
		threadPool = new PersonStatusUpdateThread[MAX_THREADS];
		curThreads = 0;
		createThreads();
	}
	
	private void createThreads(){
		for(int i = 0; i < MAX_THREADS; i++){
			threadPool[i] = new PersonStatusUpdateThread();
			threadPool[i].start();
		}
	}
	
	public synchronized void updateStatus(EventCollection events){
		threadPool[curThreads % MAX_THREADS].setEvents(events);
		synchronized(threadPool[curThreads % MAX_THREADS]){
			threadPool[curThreads % MAX_THREADS].notifyAll();
		}
		curThreads++;
	}
	
	public synchronized void terminate(){
		for(int i = 0; i < MAX_THREADS; i++){
			threadPool[i].terminate();
			synchronized(threadPool[i]){
				threadPool[i].notifyAll();
			}
		}
	}
}
